<template>
  <!-- 空间资源使用分析 -->
  <div id="space-usage-analyze">
    <a-card title="空间资源使用">
      <a-flex gap="middle">
        <a-card title="存储空间" style="width: 50%">
          <div class="inner-box" style="height: 320px; text-align: center">
            <h3>
              {{ formatSize(data.usedSize) }} /
              {{ data.maxSize ? formatSize(data.maxSize) : '无限制' }}
            </h3>
            <a-progress type="dashboard" :percent="data.sizeUsageRatio ?? 0" />
          </div>
        </a-card>
        <a-card title="图片数量" style="width: 50%">
          <div class="inner-box" style="height: 320px; text-align: center">
            <h3>
              {{ formatSize(data.usedCount) }} /
              {{ data.maxCount ? formatSize(data.maxCount) : '无限制' }}
            </h3>
            <a-progress type="dashboard" :percent="data.countUsageRatio ?? 0" />
          </div>
        </a-card>
      </a-flex>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { ref, withDefaults, defineProps, onMounted } from 'vue'
import { getSpaceUsageAnalyzeUsingPost } from '@/request/api/spaceAnalyzeController.ts'
import { message } from 'ant-design-vue'
import { formatSize } from '@/utils'

interface Props {
  queryAll?: boolean
  queryPublic?: boolean
  spaceId?: number
}

const props = withDefaults(defineProps<Props>(), {
  queryAll: false,
  queryPublic: false,
})

const data = ref<API.SpaceUsageAnalyzeResponse>({})
const loading = ref(false)

const fetchData = async () => {
  loading.value = true
  const { data: res } = await getSpaceUsageAnalyzeUsingPost({
    queryAll: props.queryAll,
    queryPublic: props.queryPublic,
    spaceId: props.spaceId,
  })
  if (res.code === 0 && res.data) {
    data.value = res.data
  } else {
    message.error('获取数据失败,' + res.message)
  }
  loading.value = false
}
onMounted(() => {
  fetchData()
})
</script>

<style lang="less" scoped></style>
